package com.yanqu.road.dao.impl.nenmonopolymarket.resgrab;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.systemgroup.newmonopolymarket.resgrab.NewMonopolyMarketBaseBattleObjectInfo;
import com.yanqu.road.entity.systemgroup.newmonopolymarket.resgrab.NewMonopolyMarketBeautyInfo;
import com.yanqu.road.entity.systemgroup.newmonopolymarket.resgrab.NewMonopolyMarketPatronsInfo;
import com.yanqu.road.entity.systemgroup.newmonopolymarket.resgrab.NewMonopolyMarketResGrabPointData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NewMonopolyMarketResGrabPointDataDaoImpl extends TempDataObjectDao<NewMonopolyMarketResGrabPointData> {

    public static String getNewMonopolyMarketResGrabPointDataSql = "select * from `t_c_new_monopoly_market_res_grab_point` where `weekId`=? and `groupId`=?";
    public List<NewMonopolyMarketResGrabPointData> getNewMonopolyMarketResGrabPointData(int weekId, long groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<NewMonopolyMarketResGrabPointData> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getNewMonopolyMarketResGrabPointDataSql);
            int i = 1;
            call.setInt(i++, weekId);
            call.setLong(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                NewMonopolyMarketResGrabPointData data = parse(rs);
                list.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String updateNewMonopolyMarketResGrabPointDataSql = "update `t_c_new_monopoly_market_res_grab_point` set type=?,userId=?,remainTime=?,receiveLastTime=?,produceRewardLastTime=?,seatTime=?,endTime=?," +
            "totalBlood=?,blood=?,objectInfoList=?,changeTime=?,resetTime=?,waitTimeout=?,serverId=? where weekId=? and groupId=? and resGroupId=? and posIndex=?";
    @Override
    protected boolean updateData(NewMonopolyMarketResGrabPointData newMonopolyMarketResGrabPointData) {
        newMonopolyMarketResGrabPointData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getType()));
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketResGrabPointData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getRemainTime()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getReceiveLastTime()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getProduceRewardLastTime()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getSeatTime()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getEndTime()));
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketResGrabPointData.getTotalBlood()));
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketResGrabPointData.getBlood()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(newMonopolyMarketResGrabPointData.getObjectInfoList())));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getChangeTime()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getWaitTimeout()));
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketResGrabPointData.getServerId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getWeekId()));
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketResGrabPointData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getResGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getPosIndex()));
        if (getDb().executeNoneQuery(updateNewMonopolyMarketResGrabPointDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertNewMonopolyMarketResGrabPointDataSql = "insert into `t_c_new_monopoly_market_res_grab_point`(weekId,groupId,resGroupId,posIndex,type,userId,remainTime,receiveLastTime,produceRewardLastTime,seatTime,endTime," +
            "totalBlood,blood,objectInfoList,changeTime,resetTime,waitTimeout,serverId) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(NewMonopolyMarketResGrabPointData newMonopolyMarketResGrabPointData) {
        newMonopolyMarketResGrabPointData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getWeekId()));
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketResGrabPointData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getResGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getPosIndex()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getType()));
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketResGrabPointData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getRemainTime()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getReceiveLastTime()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getProduceRewardLastTime()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getSeatTime()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getEndTime()));
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketResGrabPointData.getTotalBlood()));
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketResGrabPointData.getBlood()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(newMonopolyMarketResGrabPointData.getObjectInfoList())));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getChangeTime()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getWaitTimeout()));
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketResGrabPointData.getServerId()));
        if (getDb().executeNoneQuery(insertNewMonopolyMarketResGrabPointDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected NewMonopolyMarketResGrabPointData parseData(ResultSet rs) throws SQLException {
        NewMonopolyMarketResGrabPointData newMonopolyMarketResGrabPointData = new NewMonopolyMarketResGrabPointData();
        newMonopolyMarketResGrabPointData.setWeekId(rs.getInt("weekId"));
        newMonopolyMarketResGrabPointData.setGroupId(rs.getLong("groupId"));
        newMonopolyMarketResGrabPointData.setResGroupId(rs.getInt("resGroupId"));
        newMonopolyMarketResGrabPointData.setPosIndex(rs.getInt("posIndex"));
        newMonopolyMarketResGrabPointData.setType((byte)rs.getInt("type"));
        newMonopolyMarketResGrabPointData.setUserId(rs.getLong("userId"));
        newMonopolyMarketResGrabPointData.setRemainTime(rs.getInt("remainTime"));
        newMonopolyMarketResGrabPointData.setReceiveLastTime(rs.getInt("receiveLastTime"));
        newMonopolyMarketResGrabPointData.setProduceRewardLastTime(rs.getInt("produceRewardLastTime"));
        newMonopolyMarketResGrabPointData.setSeatTime(rs.getInt("seatTime"));
        newMonopolyMarketResGrabPointData.setEndTime(rs.getInt("endTime"));
        newMonopolyMarketResGrabPointData.setTotalBlood(rs.getLong("totalBlood"));
        newMonopolyMarketResGrabPointData.setBlood(rs.getLong("blood"));

        if(newMonopolyMarketResGrabPointData.getType() == 12){
            List<NewMonopolyMarketPatronsInfo> list = (JSON.parseObject(rs.getString("objectInfoList"), new TypeReference<List<NewMonopolyMarketPatronsInfo>>() {}));
            newMonopolyMarketResGrabPointData.getObjectInfoList().addAll(list);
        }else if(newMonopolyMarketResGrabPointData.getType() == 13){
            List<NewMonopolyMarketBeautyInfo> list = (JSON.parseObject(rs.getString("objectInfoList"), new TypeReference<List<NewMonopolyMarketBeautyInfo>>() {}));
            newMonopolyMarketResGrabPointData.getObjectInfoList().addAll(list);
        }
        newMonopolyMarketResGrabPointData.setChangeTime(rs.getInt("changeTime"));
        newMonopolyMarketResGrabPointData.setResetTime(rs.getInt("resetTime"));
        newMonopolyMarketResGrabPointData.setWaitTimeout(rs.getInt("waitTimeout"));
        newMonopolyMarketResGrabPointData.setServerId(rs.getLong("serverId"));
        return newMonopolyMarketResGrabPointData;
    }


    /**
     * 批量更新据点信息
     * @param list
     * @return
     */
    public boolean batchUpdate(List<NewMonopolyMarketResGrabPointData> list){
        getDb().setConnName(DbNameStrategy.getCross2Db());
        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        for (NewMonopolyMarketResGrabPointData newMonopolyMarketResGrabPointData : list) {
            Map<Integer, DbParameter> para = new HashMap<>();
            int i = 1;
            newMonopolyMarketResGrabPointData.setBeginWriteTime();
            para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getType()));
            para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketResGrabPointData.getUserId()));
            para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getRemainTime()));
            para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getReceiveLastTime()));
            para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getProduceRewardLastTime()));
            para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getSeatTime()));
            para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getEndTime()));
            para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketResGrabPointData.getTotalBlood()));
            para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketResGrabPointData.getBlood()));
            para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(newMonopolyMarketResGrabPointData.getObjectInfoList())));
            para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getChangeTime()));
            para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getResetTime()));
            para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getWaitTimeout()));
            para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketResGrabPointData.getServerId()));
            para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getWeekId()));
            para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketResGrabPointData.getGroupId()));
            para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getResGroupId()));
            para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getPosIndex()));
            mapList.add(para);
            newMonopolyMarketResGrabPointData.setNoneOption();
        }
        return getDb().executeBatchQuery(updateNewMonopolyMarketResGrabPointDataSql, mapList) > -1;
    }

    /**
     * 批量更新据点信息
     * @param list
     * @return
     */
    public boolean batchAdd(List<NewMonopolyMarketResGrabPointData> list){
        getDb().setConnName(DbNameStrategy.getCross2Db());
        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        for (NewMonopolyMarketResGrabPointData newMonopolyMarketResGrabPointData : list) {
            Map<Integer, DbParameter> para = new HashMap<>();
            int i = 1;
            para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getWeekId()));
            para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketResGrabPointData.getGroupId()));
            para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getResGroupId()));
            para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getPosIndex()));
            para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getType()));
            para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketResGrabPointData.getUserId()));
            para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getRemainTime()));
            para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getReceiveLastTime()));
            para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getProduceRewardLastTime()));
            para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getSeatTime()));
            para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getEndTime()));
            para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketResGrabPointData.getTotalBlood()));
            para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketResGrabPointData.getBlood()));
            para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(newMonopolyMarketResGrabPointData.getObjectInfoList())));
            para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getChangeTime()));
            para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getResetTime()));
            para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabPointData.getWaitTimeout()));
            para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketResGrabPointData.getServerId()));
            mapList.add(para);
            newMonopolyMarketResGrabPointData.setNoneOption();
        }
        return getDb().executeBatchQuery(insertNewMonopolyMarketResGrabPointDataSql, mapList) > -1;
    }
}
